package com.chenglin.mapdemo.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.chenglin.mapdemo.DisplayUtil;
import com.chenglin.mapdemo.R;
import com.chenglin.mapdemo.adapter.LayerAdapter;
import com.chenglin.mapdemo.bean.LayerBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 多图层弹窗  dialog 使用 RecyclerView 嵌套RecyclerView 实现的
 */
public class LayerDialog extends Dialog {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private Context mContext;

    private List<LayerBean> list;
    private LayerDialogCallBack callBack;
    private LayerAdapter layerAdapter;


    public LayerDialog(@NonNull Context context,  List<LayerBean> list, LayerDialogCallBack callBack) {
        super(context, R.style.DialogStyle);
        this.mContext = context;
        this.list = list;
        this.callBack = callBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layer);
        ButterKnife.bind(this);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        window.setGravity(Gravity.TOP | Gravity.RIGHT);
        int top = DisplayUtil.dip2px(mContext, (48+20+14));   //弹窗距离上部的距离(需要自己计算)
        int right = DisplayUtil.dip2px(mContext, (20+48+10));  // 距离左边的距离(需要自己计算)
        window.getDecorView().setPadding(0, top, right, 0);
        attributes.width = WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setDimAmount(0f);   //关闭遮罩
        window.setAttributes(attributes);

        initView();
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));


        layerAdapter = new LayerAdapter(mContext, list, new LayerAdapter.LayerAdaterCallBack() {
            @Override
            public void onItemCallBack(LayerBean.InnerBean bean, int position, int innerPosition) {
                if (position != 2) {
                    for (int i = 0; i < list.size()-1; i++) {
                        for (int j = 0; j < list.get(i).getInner().size() ; j++) {
                            list.get(i).getInner().get(j).setSelect(false);
                        }
                    }
                    list.get(position).getInner().get(innerPosition).setSelect(true);

                } else {
                    //专题地图 可以多选
                    List<LayerBean.InnerBean> inner = list.get(position).getInner();
                    for (int i = 0; i < inner.size(); i++) {
                        if (innerPosition == i) {
                            if (inner.get(i).getSelect()) {
                                inner.get(i).setSelect(false);
                            } else {
                                inner.get(i).setSelect(true);
                            }
                        }

                    }

                }
                layerAdapter.notifyDataSetChanged();
                callBack.onClickLisenter(bean);
            }
        });
        //列表点击事件回调
        recyclerView.setAdapter(layerAdapter);
    }


    public interface LayerDialogCallBack{
        void onClickLisenter(LayerBean.InnerBean bean);
    }
}
